package com.report.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.PosterReq;
import com.report.dto.response.PosterRes;
import com.report.entity.Poster;
import com.report.mapper.PosterMapper;
import com.report.service.PosterService;
import com.zmh.framework.oss.OssConfigure;
import com.zmh.framework.util.Base64ToMultipartFile;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author summer
 * @date 2024-07-03 14:58
 */
@Service
@Transactional
@AllArgsConstructor
public class PosterServiceImpl extends ServiceImpl<PosterMapper, Poster> implements PosterService {

    private final OssConfigure ossConfigure;

    @Override
    public void savePoster(PosterReq posterReq) {
        Poster convert = Convert.convert(Poster.class, posterReq);
        if(posterReq.getShowImage()){
            MultipartFile multipartFile = Base64ToMultipartFile.base64ToMultipart(posterReq.getBase64());
            String fileName = ossConfigure.uploadFile(multipartFile);
            convert.setImage(ossConfigure.getAccessUrl()+fileName);

        }
        this.saveOrUpdate(convert);
    }

    @Override
    public Page<PosterRes> lists(PosterReq posterReq) {
        Page<Poster> page=new Page<>(posterReq.getPageNum(),posterReq.getPageSize());
        LambdaQueryWrapper<Poster> posterLambdaQueryWrapper = Wrappers.lambdaQuery(Poster.class).like(StrUtil.isNotEmpty(posterReq.getName()),Poster::getName, posterReq.getName()).orderByDesc(Poster::getCreateTime);
        page=this.page(page,posterLambdaQueryWrapper);
        return Convert.convert(new TypeReference<Page<PosterRes>>() {},page);
    }
}
